<?php

namespace backend\controllers;

use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use Yii;

class SuperController extends Controller {
    /* 通用成功跳转  

     * @param unknown $url 成功后跳转的URL

     * @param number $sec 自动跳转秒数

     * @return Ambigous <string, string>

     */

    public function success($url = [], $sec = 2)
    {

        $url = empty($url) ? [ 'index' ] : $url;

        $url = \yii\helpers\Url::toRoute($url);
        $this->layout = "main-login";
        return $this->render('../base/msg', [ 'gotoUrl' => $url, 'sec' => $sec ]);
    }

    /**

     * 通用错误跳转

     * @param string $msg 错误提示信息

     * @param number $sec

     * @return Ambigous <string, string>

     */
    public function error($msg = '', $url = [], $sec = 2)
    {
        $this->layout = "main-login";
        if (empty($url)) {
            return $this->render('../base/msg', [ 'errorMessage' => $msg, 'sec' => $sec ]);
        }
        else {
            $url = empty($url) ? [ 'index' ] : $url;
            $url = \yii\helpers\Url::toRoute($url);
            return $this->render('../base/msg', [ 'gotoUrl' => $url, 'sec' => $sec, 'errorMessage' => $msg ]);
        }
    }

    /**
     * 在程序执行之前，对访问的方法进行权限验证.
     * @param \yii\base\Action $action
     * @return bool
     * @throws ForbiddenHttpException
     */
    public function beforeAction($action)
    {
        //如果未登录，则直接返回;
        if ($action->controller->id == 'site') {
            return parent::beforeAction($action);
        }
        if (Yii::$app->user->isGuest) {
            return $this->goHome();
        }
         return parent::beforeAction($action);
        //获取路径
//        $path = Yii::$app->request->pathInfo;
//
//        //忽略列表
//        if (in_array($path, $this->ignoreList)) {
//            return true;
//        }
//
//        if (Yii::$app->user->can($path)) {
//            return true;
//        }
//        else {
//            throw new ForbiddenHttpException(Yii::t('app', 'message 401'));
//        }
    }

}
